function spike_count(varargin)

% % spike count gets the mean and stdev of the spikes elicited in the last 5 records
% of a block (actually, 10 alternate records). 
% 
% based on basic template file for data analysis routines.
% gets the current datac window selection, and does the analysis
% for all selected record groups/blocks
% automatically refreshes the data arrays from the disk file if necessary
% and also shows how to generate a figure.
% Plugin_template2 shows how to  access the main database rather than
% specific data files/data (in other words, how to summarize analyzed data)
% 8/06 P. Manis

% the main routine is just a wrapper to handle the multiple selection and
% catch execution errors.
%
% Note: when testing, set the testflag to 1 so that we DO NOT do try/catch.
% you need to do this to catch the errors in the _go routine.
%
global CONTROL

if(nargin == 0)
    noq = 0;
else
    noq = varargin{1};
end;

sf = getmainselection; % read the list of selected traces
testflag = 1;

if(sf > 0)
    pflag = getplotflag; % read teh plot flag (used in the routine template2)
    
    QueMessage('Template for plugin anaysis code', 1); % clear the que
    for i = 1:length(sf)
        if(noq == 0)
            CONTROL(sf(i)).basechan = []; % clear the base channel for analysis...
        end;
        if(testflag == 0)
            try
                template_go(sf(i), pflag, noq);
            catch
                fprintf(1, 'Plugin Template Failed execution for sf = %d\n  ***[File: %s, records: %d - %d ]\n', ...
                    sf(i), CONTROL(sf(i)).filename, CONTROL(sf(i)).recbeg, CONTROL(sf(i)).recend);
            end;
        else
            template_go(sf(i), 0, noq); % also turn off plot flag when testing
        end;

    end;
end;



%--------------The real analysis routine------------------

function template_go(sf, pflag, noq)

% access to all of the data and parameters
global CONTROL

% only do the analysis if:
% 1. the structure does not already exist,

if(isempty(CONTROL(sf).tc_int))
    QueMessage('tc_int not analyzed for %s', CONTROL(sf).filename);
    return;
end;

x=CONTROL(sf).tc_int;
y = x(sf).nspike;
y = y(end-9:2:end);
ym = mean(y);
ys = std(y);

% get spike counts
QueMessage(sprintf('Plugin Template Example - found %d spikes', length(z)));

